#include "stdafx.h"
#include "CLayoutUI.h"
#include "DuiDemo.h"
#include "CMainFrame.h"
#include "CViewContainer.h"

CLayoutUI::CLayoutUI(HWND hParent /*= NULL*/)
{
	Create(hParent, _T("LayoutUI"), UI_WNDSTYLE_FRAME, NULL, 0, 0, 0, 0);
}

CLayoutUI::~CLayoutUI()
{

}

LPCTSTR CLayoutUI::GetWindowClassName() const
{
	return _T("LayoutUI");
}

void CLayoutUI::OnFinalMessage(HWND hWnd)
{

}

void CLayoutUI::InitWindow()
{

}

DuiLib::DString CLayoutUI::GetSkinFile()
{
	return _T("layout.xml");
}

DuiLib::DString CLayoutUI::GetSkinFolder()
{
	return DString(CPaintManagerUI::GetInstancePath()) + _T("skin\\");
}

DuiLib::CControlUI* CLayoutUI::CreateControl(LPCTSTR pstrClass)
{
	return NULL;
}

LRESULT CLayoutUI::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_KILLFOCUS)
	{
		ShowWindow(SW_HIDE);
		//Close(0);
	}
	else if (uMsg == WM_KEYDOWN)
	{
		if (wParam == VK_ESCAPE)
		{
			ShowWindow(SW_HIDE);
			return 1;
		}
	}
	else if (uMsg == WM_MOUSEMOVE)
	{
		POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
		//::ClientToScreen(GetHWND(), &pt);
		//::ScreenToClient(g_pMainFrame->GetHWND(), &pt);

		RECT wndPos;
		GetWindowRect(GetHWND(), &wndPos);
		POINT ptLeft;
		ptLeft.x = wndPos.left;
		ptLeft.y = wndPos.top;
		::ScreenToClient(GetHWND(), &ptLeft);

		RECT rc;

		rc.left = ptLeft.x;
		rc.top = ptLeft.y;
		rc.right = pt.x;
		rc.bottom = pt.y;

		RECT rcIn;
		int maxIdx = -1;

		for (int i = 0; i < m_labels->GetSize(); i++)
		{
			CLabelUI* pLabel = static_cast<CLabelUI*>(m_labels->GetAt(i));
			RECT pos = pLabel->GetPos();
			if (::IntersectRect(&rcIn, &pos, &rc))
			{
				pLabel->SetBkColor(RGB(180, 180, 180) | 0xFF000000);
				pLabel->SetBorderSize(1);
				pLabel->SetBorderColor(RGB(230, 230, 230) | 0xFF000000);
				maxIdx = i > maxIdx ? i : maxIdx;
			}
			else
			{
				pLabel->SetBkColor(RGB(130, 130, 130) | 0xFF000000);
				pLabel->SetBorderSize(0);
			}
			pLabel->SetText(_T(""));
		}

		if (maxIdx > -1 && maxIdx < m_labels->GetSize()) 
		{
			CLabelUI* pLabel = static_cast<CLabelUI*>(m_labels->GetAt(maxIdx));
			DString name = pLabel->GetName();
			name.Replace(_T("L"), _T(""));
			name.Replace(_T("_"), _T("X"));
			pLabel->SetText(name);
			pLabel->SetTextColor(RGB(255,255,255));
			pLabel->SetTextPadding(CDuiRect(6,0,0,0));

		}
	}
	else if (uMsg == WM_LBUTTONDOWN)
	{
		POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };

		for (int i = 0; i < m_labels->GetSize(); i++)
		{
			CLabelUI* pLabel = static_cast<CLabelUI*>(m_labels->GetAt(i));
			RECT pos = pLabel->GetPos();
			if (::PtInRect(&pos, pt))
			{
				DString name = pLabel->GetName();
				name.Replace(_T("L"), _T(""));
				int pos = name.Find(_T("_"));
				DString r = name.Left(pos);
				DString c = name.Mid(pos + 1);
				int row = _ttoi(r);
				int col = _ttoi(c);
				//int num = _ttoi(name.GetData());
				//int row = num / 10;
				//int col = num % 10;
				g_pMainFrame->m_pContainer->ClearSplit();
				g_pMainFrame->m_pContainer->SetLayout(row, col);
				ShowWindow(SW_HIDE);
				break;
			}
			
		}

	}

	return __super::HandleMessage(uMsg, wParam, lParam);
}

void CLayoutUI::OnPrepare(TNotifyUI& msg)
{
	m_labels = m_PaintManager.FindSubControlsByClass(NULL, _T("Label"));
}

void CLayoutUI::Notify(TNotifyUI& msg)
{
	if (msg.sType == _T("windowinit")) OnPrepare(msg);
	else if (msg.sType == _T("click"))
	{

	}
	
}
